package com.lbj.mall.product.controller;

import com.lbj.mall.common.utils.PageUtils;
import com.lbj.mall.common.utils.R;
import com.lbj.mall.common.validator.group.AddGroup;
import com.lbj.mall.product.entity.BrandEntity;
import com.lbj.mall.product.service.BrandService;
import com.lbj.mall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * 品牌
 *
 * @author libingjun
 * @email 617416791@qq.com
 * @date 2022-03-27 10:44:55
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 真正的保存方法，统一由ControllerAdvice处理异常
     */
    @RequestMapping("save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand) {
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 保存-在控制器里面控制异常错误
     * // @RequiresPermissions("product:brand:save")
     */
    @RequestMapping("/save/LocalCheck")
    public R saveLocalCheck(@Valid @RequestBody BrandEntity brand, BindingResult bindingResult) {
        // 1. @Valid 表示开启校验
        // 2. @RequestBody 从请求体中取数据
        // 3. BindingResult 验证的结果

        // 如果验证有问题
        if (bindingResult.hasErrors()) {
            Map<String, String> errorMap = new HashMap<>();
            bindingResult.getFieldErrors().forEach(item -> {
                String field = item.getField();
                String message = item.getDefaultMessage();
                errorMap.put(field, message);
            });
            return R.error(400, "数据校验失败").put("data", errorMap);
        } else {
            brandService.save(brand);
            return R.ok();
        }
    }

    /**
     * 修改
     * <p>
     * 如果修改了品牌的名称，则需要修改所有与品牌名相关的数据
     */
    @Transactional
    @RequestMapping("/update")
    // @RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        // 如果要修改品牌名，则需要修改和品牌名相关的所有数据
        if (!StringUtils.isEmpty(brand.getName())) {
            categoryBrandRelationService.updateBrandName(brand.getBrandId(), brand.getName());

            // TODO 修改与品牌名所有有关的数据
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
